<?php

namespace App\Admin\Controllers;

use App\Admin\Requests\MenuCreateRequest;
use App\Admin\Services\GrantService;
use App\Admin\Services\MenuService;
use Illuminate\Support\Arr;

class MenuController extends Controller
{
    protected $menuService;
    protected $grantService;

    public function __construct()
    {
        parent::__construct();
        $this->grantService = new GrantService();
        $this->menuService  = new MenuService();
    }

    /**
     * 菜單列表
     */
    public function index()
    {
        $menu = $this->menuService->getMenusTree();
        return view('admin.menu.index', ['menus' => $menu]);
    }

    /**
     * 生成菜單
     */
    public function store(MenuCreateRequest $request)
    {
        $this->menuService->store($request->all());
        return $this->success('操作成功');
    }

    /**
     * 編輯菜單
     */
    public function edit()
    {
        $id      = request()->id;
        $menu    = $this->menuService->getMenus();
        $ownMenu = $this->menuService->findMenu($id);

        return view('admin.menu.menu_edit', [
            'menu'    => $menu,
            'ownMenu' => $ownMenu,
        ]);
    }

    /**
     * 更新菜單
     */
    public function update(MenuCreateRequest $request)
    {
        $this->menuService->update(Arr::only($request->all(), ['id', 'parent_id', 'sort', 'name', 'icon', 'uri']));
        return $this->success('操作成功', [], 200, route('menu.index', null, false));
    }

    /**
     * 刪除
     */
    public function delete()
    {
        $id = request()->id;
        $this->menuService->delete($id);
        return $this->success('刪除成功', [], 200, route('menu.index', null, false));
    }
}
